﻿using System;
using System.Diagnostics;
using System.Globalization;

namespace FastNet.Internal
{
	internal static class Logic
	{
		[DebuggerNonUserCode]
		public static void Check<TException>( Boolean condition ) where TException : Exception
		{
			if ( !condition )
			{
				throw (TException)Activator.CreateInstance( typeof( TException ) );
			}
		}


		[DebuggerNonUserCode]
		public static void Check<TException>( Boolean condition, String message ) where TException : Exception
		{
			if ( !condition )
			{
				throw (TException)Activator.CreateInstance( typeof( TException ), message );
			}
		}


		[DebuggerNonUserCode]
		public static void Check<TException>( Boolean condition, String message, Object arg0 ) where TException : Exception
		{
			if ( !condition )
			{
				throw (TException)Activator.CreateInstance( typeof( TException ), String.Format( s_formatProvider, message, arg0 ) );
			}
		}


		[DebuggerNonUserCode]
		public static void Check<TException>( Boolean condition, String message, Object arg0, Object arg1 ) where TException : Exception
		{
			if ( !condition )
			{
				throw (TException)Activator.CreateInstance( typeof( TException ), String.Format( s_formatProvider, message, arg0, arg1 ) );
			}
		}


		[DebuggerNonUserCode]
		public static void Check<TException>( Boolean condition, String message, Object arg0, Object arg1, Object arg2 ) where TException : Exception
		{
			if ( !condition )
			{
				throw (TException)Activator.CreateInstance( typeof( TException ), String.Format( s_formatProvider, message, arg0, arg1, arg2 ) );
			}
		}


		[DebuggerNonUserCode]
		public static void Check<TException>( Boolean condition, String message, params Object[] data ) where TException : Exception
		{
			if ( !condition )
			{
				throw (TException)Activator.CreateInstance( typeof( TException ), String.Format( s_formatProvider, message, data ) );
			}
		}


		[DebuggerNonUserCode]
		public static void Assert( Boolean condition )
		{
			Debug.Assert( condition );

			if ( !condition )
			{
				throw new InvalidProgramException( "Assertion Failed." );
			}
		}


		[DebuggerNonUserCode]
		public static void Assert( Boolean condition, String message )
		{
			Debug.Assert( condition, message );

			if ( !condition )
			{
				throw new InvalidProgramException( "Assertion Failed. " + message );
			}
		}


		private static readonly IFormatProvider s_formatProvider = CultureInfo.GetCultureInfo( "en-US" );
	}
}
